<!DOCTYPE html><html lang="en" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>总结了38个ES6-ES12的开发技巧 | 孙志豪的博客</title><meta name="author" content="孙志豪"><meta name="copyright" content="孙志豪"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="总结了38个ES6-ES12的开发技巧ES61、let 和 const这两个的出现，总感觉是为了开发的代码规范而出现的。我们要逐渐放弃var，在项目中多用let和const 与var的区别：  var有变量提升，有初始化提升，值可变 let有变量提升，没有初始化提升，值可变 const有变量提升，没有初始化提升，值不可变，但如果是定义对象，则属性可变  暂时性死区问题说明：其实let和const">
<meta property="og:type" content="article">
<meta property="og:title" content="总结了38个ES6-ES12的开发技巧">
<meta property="og:url" content="https://szhihao.gitee.io/2022/10/11/06-%E6%A0%B8%E5%BF%83js%EF%BC%88%E9%AA%9A%E5%8C%85%E6%8A%80%E5%B7%A7%E7%AF%87%EF%BC%89/%E6%80%BB%E7%BB%93%E4%BA%8638%E4%B8%AAES6-ES12%E7%9A%84%E5%BC%80%E5%8F%91%E6%8A%80%E5%B7%A7/index.html">
<meta property="og:site_name" content="孙志豪的博客">
<meta property="og:description" content="总结了38个ES6-ES12的开发技巧ES61、let 和 const这两个的出现，总感觉是为了开发的代码规范而出现的。我们要逐渐放弃var，在项目中多用let和const 与var的区别：  var有变量提升，有初始化提升，值可变 let有变量提升，没有初始化提升，值可变 const有变量提升，没有初始化提升，值不可变，但如果是定义对象，则属性可变  暂时性死区问题说明：其实let和const">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://szhihao.gitee.io/img/%E5%A4%A9%E7%A9%BA.jpeg">
<meta property="article:published_time" content="2022-10-11T12:01:36.000Z">
<meta property="article:modified_time" content="2022-10-11T09:29:43.427Z">
<meta property="article:author" content="孙志豪">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://szhihao.gitee.io/img/%E5%A4%A9%E7%A9%BA.jpeg"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://szhihao.gitee.io/2022/10/11/06-%E6%A0%B8%E5%BF%83js%EF%BC%88%E9%AA%9A%E5%8C%85%E6%8A%80%E5%B7%A7%E7%AF%87%EF%BC%89/%E6%80%BB%E7%BB%93%E4%BA%8638%E4%B8%AAES6-ES12%E7%9A%84%E5%BC%80%E5%8F%91%E6%8A%80%E5%B7%A7/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","languages":{"hits_empty":"We didn't find any results for the search: ${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: 'Copy successfully',
    error: 'Copy error',
    noSupport: 'The browser does not support'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: 'Just',
    min: 'minutes ago',
    hour: 'hours ago',
    day: 'days ago',
    month: 'months ago'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '总结了38个ES6-ES12的开发技巧',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-10-11 17:29:43'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "" data-lazy-src="/img/%E5%A4%B4%E5%83%8F.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">Articles</div><div class="length-num">44</div></a><a href="/tags/"><div class="headline">Tags</div><div class="length-num">1</div></a><a href="/categories/"><div class="headline">Categories</div><div class="length-num">15</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-graduation-cap"></i><span> 博文</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/categories/"><i class="fa-fw fa fa-archive"></i><span> 分类</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/archives/"><i class="fa-fw fa fa-folder-open"></i><span> 归档</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 生活</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/photos/"><i class="fa-fw fa fa-camera-retro"></i><span> 相册</span></a></li><li><a class="site-page child" href="/music/"><i class="fa-fw fa fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 影视</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/links/"><i class="fa-fw fa fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/comment/"><i class="fa-fw fa fa-paper-plane"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于笔者</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/img/%E5%A4%A9%E7%A9%BA.jpeg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">孙志豪的博客</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> Search</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-graduation-cap"></i><span> 博文</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/categories/"><i class="fa-fw fa fa-archive"></i><span> 分类</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/archives/"><i class="fa-fw fa fa-folder-open"></i><span> 归档</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 生活</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/photos/"><i class="fa-fw fa fa-camera-retro"></i><span> 相册</span></a></li><li><a class="site-page child" href="/music/"><i class="fa-fw fa fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 影视</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/links/"><i class="fa-fw fa fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/comment/"><i class="fa-fw fa fa-paper-plane"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于笔者</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">总结了38个ES6-ES12的开发技巧</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">Created</span><time class="post-meta-date-created" datetime="2022-10-11T12:01:36.000Z" title="Created 2022-10-11 20:01:36">2022-10-11</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">Updated</span><time class="post-meta-date-updated" datetime="2022-10-11T09:29:43.427Z" title="Updated 2022-10-11 17:29:43">2022-10-11</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%A0%B8%E5%BF%83js%EF%BC%88%E9%AA%9A%E5%8C%85%E6%8A%80%E5%B7%A7%E7%AF%87%EF%BC%89/">核心js（骚包技巧篇）</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="总结了38个ES6-ES12的开发技巧"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">Post View:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><hr>
<h1 id="总结了38个ES6-ES12的开发技巧"><a href="#总结了38个ES6-ES12的开发技巧" class="headerlink" title="总结了38个ES6-ES12的开发技巧"></a>总结了38个ES6-ES12的开发技巧</h1><h2 id="ES6"><a href="#ES6" class="headerlink" title="ES6"></a>ES6</h2><h3 id="1、let-和-const"><a href="#1、let-和-const" class="headerlink" title="1、let 和 const"></a>1、let 和 const</h3><p>这两个的出现，总感觉是为了开发的代码规范而出现的。我们要逐渐放弃var，在项目中多用let和const</p>
<p>与var的区别：</p>
<ul>
<li>var有变量提升，有初始化提升，值可变</li>
<li>let有变量提升，没有初始化提升，值可变</li>
<li>const有变量提升，没有初始化提升，值不可变，但如果是定义对象，则属性可变</li>
</ul>
<p><code>暂时性死区</code>问题说明：其实<code>let和const</code>是有变量提升的，但是没有初始化提升：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> name = <span class="string">&#x27;林三心&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">fn</span> () &#123;</span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(name)</span><br><span class="line">  <span class="keyword">let</span> name = <span class="string">&#x27;sunshin_lin&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="title function_">fn</span>() <span class="comment">// Cannot access &#x27;name&#x27; before initialization</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>块级作用域解决问题：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span>(<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="number">5</span>; i++) &#123;</span><br><span class="line">  <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(i)</span><br><span class="line">  &#125;)</span><br><span class="line">&#125; <span class="comment">// 5 5 5 5 5</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; <span class="number">5</span>; i++) &#123;</span><br><span class="line">  <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(i)</span><br><span class="line">  &#125;)</span><br><span class="line">&#125; <span class="comment">// 0 1 2 3 4</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="2、默认参数"><a href="#2、默认参数" class="headerlink" title="2、默认参数"></a>2、默认参数</h3><p>开发中你曾遇到过这样的问题，如果参数不传进来，你就设置默认参数</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">fn</span> (name, age) &#123;</span><br><span class="line">  <span class="keyword">var</span> name = name || <span class="string">&#x27;林三心&#x27;</span></span><br><span class="line">  <span class="keyword">var</span> age = age || <span class="number">18</span></span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(name, age)</span><br><span class="line">&#125;</span><br><span class="line"><span class="title function_">fn</span>() <span class="comment">// 林三心 18</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>但是这么写确实不优雅，可以使用ES6的默认参数</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">fn</span> (name = <span class="string">&#x27;林三心&#x27;</span>, age = <span class="number">18</span>) &#123;</span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(name, age)</span><br><span class="line">&#125;</span><br><span class="line"><span class="title function_">fn</span>() <span class="comment">// 林三心 18</span></span><br><span class="line"><span class="title function_">fn</span>(<span class="string">&#x27;sunshine&#x27;</span>, <span class="number">22</span>) <span class="comment">// sunshine 22</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="3、扩展运算符"><a href="#3、扩展运算符" class="headerlink" title="3、扩展运算符"></a>3、扩展运算符</h3><p>曾经的我，想要拼接多个数组，我只能这么做</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr1 = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">4</span>]</span><br><span class="line"><span class="keyword">const</span> arr2 = [<span class="number">4</span>, <span class="number">5</span>, <span class="number">7</span>]</span><br><span class="line"><span class="keyword">const</span> arr3 = [<span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>]</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> arr = arr1.<span class="title function_">concat</span>(arr2).<span class="title function_">concat</span>(arr3)</span><br><span class="line">[</span><br><span class="line">  <span class="number">1</span>, <span class="number">2</span>, <span class="number">4</span>, <span class="number">4</span>, <span class="number">5</span>,</span><br><span class="line">  <span class="number">7</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span></span><br><span class="line">]</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>现在的我，可以更优雅地进行拼接</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr1 = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">4</span>]</span><br><span class="line"><span class="keyword">const</span> arr2 = [<span class="number">4</span>, <span class="number">5</span>, <span class="number">7</span>]</span><br><span class="line"><span class="keyword">const</span> arr3 = [<span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>]</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> arr = [...arr1, ...arr2, ...arr3]</span><br><span class="line">[</span><br><span class="line">  <span class="number">1</span>, <span class="number">2</span>, <span class="number">4</span>, <span class="number">4</span>, <span class="number">5</span>,</span><br><span class="line">  <span class="number">7</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span></span><br><span class="line">]</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="4、剩余参数"><a href="#4、剩余参数" class="headerlink" title="4、剩余参数"></a>4、剩余参数</h3><p>大家可能遇到过这种问题，一个函数，传入参数的个数是不确定的，这就可以用ES6的剩余参数</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">fn</span> (name, ...params) &#123;</span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(name)</span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(params)</span><br><span class="line">&#125;</span><br><span class="line">fn (<span class="string">&#x27;林三心&#x27;</span>, <span class="number">1</span>, <span class="number">2</span>) <span class="comment">// 林三心 [ 1, 2 ]</span></span><br><span class="line">fn (<span class="string">&#x27;林三心&#x27;</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>) <span class="comment">// 林三心 [ 1, 2, 3, 4, 5 ]</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="5、模板字符串"><a href="#5、模板字符串" class="headerlink" title="5、模板字符串"></a>5、模板字符串</h3><p>以前的我，拼接字符串只能这么做</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> name = <span class="string">&#x27;林三心&#x27;</span></span><br><span class="line"><span class="keyword">const</span> age = <span class="string">&#x27;22&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(name + <span class="string">&#x27;今年&#x27;</span> + age + <span class="string">&#x27;岁啦&#x27;</span>) <span class="comment">// 林三心今年22岁啦</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>现在我可以这么做，会更优雅</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> name = <span class="string">&#x27;林三心&#x27;</span></span><br><span class="line"><span class="keyword">const</span> age = <span class="string">&#x27;22&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">`<span class="subst">$&#123;name&#125;</span>今年<span class="subst">$&#123;age&#125;</span>岁啦`</span>) <span class="comment">// 林三心今年22岁啦</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="6、Object-keys"><a href="#6、Object-keys" class="headerlink" title="6、Object.keys"></a>6、Object.keys</h3><p>可以用来获取对象的key的集合，进而可以获得对应key的value</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> obj = &#123;</span><br><span class="line">  <span class="attr">name</span>: <span class="string">&#x27;林三心&#x27;</span>,</span><br><span class="line">  <span class="attr">age</span>: <span class="number">22</span>,</span><br><span class="line">  <span class="attr">gender</span>: <span class="string">&#x27;男&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> keys = <span class="title class_">Object</span>.<span class="title function_">keys</span>(obj)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(keys) <span class="comment">// [ &#x27;name&#x27;, &#x27;age&#x27;, &#x27;gender&#x27; ]</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="7、箭头函数"><a href="#7、箭头函数" class="headerlink" title="7、箭头函数"></a>7、箭头函数</h3><p>以前我们使用普通函数</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">fn</span> () &#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> fn = <span class="keyword">function</span> (<span class="params"></span>) &#123;&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>ES6新加了<code>箭头函数</code></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> <span class="title function_">fn</span> = (<span class="params"></span>) =&gt; &#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 如果只有一个参数，可以省略括号</span></span><br><span class="line"><span class="keyword">const</span> <span class="title function_">fn</span> = name =&gt; &#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 如果函数体里只有一句return</span></span><br><span class="line"><span class="keyword">const</span> <span class="title function_">fn</span> = name =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">2</span> * name</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 可简写为</span></span><br><span class="line"><span class="keyword">const</span> <span class="title function_">fn</span> = name =&gt; <span class="number">2</span> * name</span><br><span class="line"><span class="comment">// 如果返回的是对象</span></span><br><span class="line"><span class="keyword">const</span> <span class="title function_">fn</span> = name =&gt; (&#123; <span class="attr">name</span>: name &#125;)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>普通函数和箭头函数的区别：</p>
<ul>
<li>1、箭头函数不可作为构造函数，不能使用new</li>
<li>2、箭头函数没有自己的this</li>
<li>3、箭头函数没有arguments对象</li>
<li>4、箭头函数没有原型对象</li>
</ul>
<h3 id="8、Array-prototype-forEach"><a href="#8、Array-prototype-forEach" class="headerlink" title="8、Array.prototype.forEach"></a>8、Array.prototype.forEach</h3><p>ES6新加的数组遍历方法</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> eachArr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment">// 三个参数：遍历项 索引 数组本身</span></span><br><span class="line"><span class="comment">// 配合箭头函数</span></span><br><span class="line">eachArr.<span class="title function_">forEach</span>(<span class="function">(<span class="params">item, index, arr</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(item, index, arr)</span><br><span class="line">&#125;)</span><br><span class="line"><span class="number">1</span> <span class="number">0</span> [ <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span> ]</span><br><span class="line"><span class="number">2</span> <span class="number">1</span> [ <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span> ]</span><br><span class="line"><span class="number">3</span> <span class="number">2</span> [ <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span> ]</span><br><span class="line"><span class="number">4</span> <span class="number">3</span> [ <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span> ]</span><br><span class="line"><span class="number">5</span> <span class="number">4</span> [ <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span> ]</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="9、Array-prototype-map"><a href="#9、Array-prototype-map" class="headerlink" title="9、Array.prototype.map"></a>9、Array.prototype.map</h3><p>常用于返回一个处理过后的新数组</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> mapArr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment">// 三个参数：遍历项 索引 数组本身</span></span><br><span class="line"><span class="comment">// 配合箭头函数，对每一个元素进行翻倍</span></span><br><span class="line"><span class="keyword">const</span> mapArr2 = mapArr.<span class="title function_">map</span>(<span class="function">(<span class="params">num, index, arr</span>) =&gt;</span> <span class="number">2</span> * num)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(mapArr2)</span><br><span class="line">[ <span class="number">2</span>, <span class="number">4</span>, <span class="number">6</span>, <span class="number">8</span>, <span class="number">10</span> ]</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="10、Array-prototype-filter"><a href="#10、Array-prototype-filter" class="headerlink" title="10、Array.prototype.filter"></a>10、Array.prototype.filter</h3><p>顾名思义，用来过滤的方法</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> filterArr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment">// 三个参数：遍历项 索引 数组本身</span></span><br><span class="line"><span class="comment">// 配合箭头函数，返回大于3的集合</span></span><br><span class="line"><span class="keyword">const</span> filterArr2 = filterArr.<span class="title function_">filter</span>(<span class="function">(<span class="params">num, index, arr</span>) =&gt;</span> num &gt; <span class="number">3</span>)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(filterArr2)</span><br><span class="line">[ <span class="number">4</span>, <span class="number">5</span> ]</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="11、Array-prototype-some"><a href="#11、Array-prototype-some" class="headerlink" title="11、Array.prototype.some"></a>11、Array.prototype.some</h3><p>some，意思就是只要有一个是真，那就返回真</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> someArr = [<span class="literal">false</span>, <span class="literal">true</span>, <span class="literal">false</span>, <span class="literal">true</span>, <span class="literal">false</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment">// 三个参数：遍历项 索引 数组本身</span></span><br><span class="line"><span class="comment">// 配合箭头函数，只要有一个为true，就返回true，一个都true都没有，就返回false</span></span><br><span class="line"><span class="keyword">const</span> someArr2 = someArr.<span class="title function_">some</span>(<span class="function">(<span class="params">bol, index, arr</span>) =&gt;</span> bol)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(someArr2)</span><br><span class="line"><span class="literal">true</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="12、Array-prototype-every"><a href="#12、Array-prototype-every" class="headerlink" title="12、Array.prototype.every"></a>12、Array.prototype.every</h3><p>every跟some是相反的，some是只要有一个就行，every是要所有为真才返回真</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> everyArr = [<span class="literal">false</span>, <span class="literal">true</span>, <span class="literal">false</span>, <span class="literal">true</span>, <span class="literal">false</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment">// 三个参数：遍历项 索引 数组本身</span></span><br><span class="line"><span class="comment">// 配合箭头函数，需要所有为true，才返回true，否则返回false</span></span><br><span class="line"><span class="keyword">const</span> everyArr2 = everyArr.<span class="title function_">every</span>(<span class="function">(<span class="params">bol, index, arr</span>) =&gt;</span> bol)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(everyArr2)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="13、Array-prototype-reduce"><a href="#13、Array-prototype-reduce" class="headerlink" title="13、Array.prototype.reduce"></a>13、Array.prototype.reduce</h3><ul>
<li>第一个参数callback函数： pre为上次return的值，next为数组的本次遍历的项</li>
<li>第二个参数为初始值，也是第一个pre</li>
</ul>
<p>举两个例子：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 计算 1 + 2 + 3 + 4 + 5</span></span><br><span class="line"><span class="keyword">const</span> reduceArr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]</span><br><span class="line"><span class="keyword">const</span> sum = reduceArr.<span class="title function_">reduce</span>(<span class="function">(<span class="params">pre, next</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> pre + next</span><br><span class="line">&#125;, <span class="number">0</span>)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(sum) <span class="comment">// 15</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 统计元素出现个数</span></span><br><span class="line"><span class="keyword">const</span> nameArr = [<span class="string">&#x27;林三心&#x27;</span>, <span class="string">&#x27;sunshine_lin&#x27;</span>, <span class="string">&#x27;林三心&#x27;</span>, <span class="string">&#x27;林三心&#x27;</span>, <span class="string">&#x27;科比&#x27;</span>]</span><br><span class="line"><span class="keyword">const</span> totalObj = nameArr.<span class="title function_">reduce</span>(<span class="function">(<span class="params">pre, next</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">if</span> (pre[next]) &#123;</span><br><span class="line">    pre[next]++</span><br><span class="line">  &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    pre[next] = <span class="number">1</span></span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> pre</span><br><span class="line">&#125;, &#123;&#125;)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(totalObj) <span class="comment">// &#123; &#x27;林三心&#x27;: 3, sunshine_lin: 1, &#x27;科比&#x27;: 1 &#125;</span></span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="14、对象属性同名简写"><a href="#14、对象属性同名简写" class="headerlink" title="14、对象属性同名简写"></a>14、对象属性同名简写</h3><p>以前同名属性需要这么写</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> name = <span class="string">&#x27;林三心&#x27;</span></span><br><span class="line"><span class="keyword">const</span> age = <span class="string">&#x27;22&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> obj = &#123;</span><br><span class="line">  <span class="attr">name</span>: name,</span><br><span class="line">  <span class="attr">age</span>: age</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(obj) <span class="comment">// &#123; name: &#x27;林三心&#x27;, age: &#x27;22&#x27; &#125;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>ES6新增语法，只需这么写</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> name = <span class="string">&#x27;林三心&#x27;</span></span><br><span class="line"><span class="keyword">const</span> age = <span class="string">&#x27;22&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 属性同名可简写</span></span><br><span class="line"><span class="keyword">const</span> obj = &#123;</span><br><span class="line">  name,</span><br><span class="line">  age</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(obj) <span class="comment">// &#123; name: &#x27;林三心&#x27;, age: &#x27;22&#x27; &#125;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="15、Promise"><a href="#15、Promise" class="headerlink" title="15、Promise"></a>15、Promise</h3><p><code>Promise</code>，中文名为<code>承诺</code>，承诺在哪呢？承诺在，一旦他的状态改变，就不会再改。这里就介绍基本使用，如果想要深入理解如何使用，请看我的另一篇文章<a target="_blank" rel="noopener" href="https://juejin.cn/post/6994594642280857630">看了就会，手写Promise原理，最通俗易懂的版本！！！</a></p>
<p>看看基本使用</p>
<ul>
<li>成功状态</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">requestData</span> () &#123;</span><br><span class="line">  <span class="comment">// 模拟请求</span></span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">      <span class="title function_">resolve</span>(<span class="string">&#x27;林三心&#x27;</span>)</span><br><span class="line">    &#125;, <span class="number">1000</span>)</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="title function_">requestData</span>().<span class="title function_">then</span>(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(res) <span class="comment">// 一秒钟后输出 &#x27;林三心&#x27;</span></span><br><span class="line">&#125;, <span class="function"><span class="params">err</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(err)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<ul>
<li>失败状态</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">requestData</span> () &#123;</span><br><span class="line">  <span class="comment">// 模拟请求</span></span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">      <span class="title function_">reject</span>(<span class="string">&#x27;错误啦&#x27;</span>)</span><br><span class="line">    &#125;, <span class="number">1000</span>)</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="title function_">requestData</span>().<span class="title function_">then</span>(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(res)</span><br><span class="line">&#125;, <span class="function"><span class="params">err</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(err) <span class="comment">// 一秒钟后输出 &#x27;错误啦&#x27;</span></span><br><span class="line">&#125;)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<ul>
<li><pre><code>all
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">  方法</span><br><span class="line"></span><br><span class="line">  - 接收一个Promise数组，数组中如有非Promise项，则此项当做成功</span><br><span class="line">  - 如果所有Promise都成功，则返回成功结果数组</span><br><span class="line">  - 如果有一个Promise失败，则返回这个失败结果</span><br><span class="line"></span><br><span class="line">```js</span><br><span class="line">// 如果全都为成功</span><br><span class="line">function fn(time) &#123;</span><br><span class="line">  return new Promise((resolve, reject) =&gt; &#123;</span><br><span class="line">    console.log(88)</span><br><span class="line">    setTimeout(() =&gt; &#123;</span><br><span class="line">      resolve(`$&#123;time&#125;毫秒后我成功啦！！！`)</span><br><span class="line">    &#125;, time)</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Promise.all([fn(2000), fn(3000), fn(1000)]).then(res =&gt; &#123;</span><br><span class="line">  // 3秒后输出 [ &#x27;2000毫秒后我成功啦！！！&#x27;, &#x27;3000毫秒后我成功啦！！！&#x27;, &#x27;1000毫秒后我成功啦！！！&#x27; ]</span><br><span class="line">  console.log(res) </span><br><span class="line">&#125;, err =&gt; &#123;</span><br><span class="line">  console.log(err)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">// 如果有一个失败</span><br><span class="line">function fn(time, isResolve) &#123;</span><br><span class="line">  return new Promise((resolve, reject) =&gt; &#123;</span><br><span class="line">    setTimeout(() =&gt; &#123;</span><br><span class="line">      isResolve ? resolve(`$&#123;time&#125;毫秒后我成功啦！！！`) : reject(`$&#123;time&#125;毫秒后我失败啦！！！`)</span><br><span class="line">    &#125;, time)</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Promise.all([fn(2000, true), fn(3000), fn(1000, true)]).then(res =&gt; &#123;</span><br><span class="line">  console.log(res)</span><br><span class="line">&#125;, err =&gt; &#123;</span><br><span class="line">  console.log(err) // 3秒后输出 &#x27;3000毫秒后我失败啦！！！&#x27;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
</code></pre>
</li>
<li><p>&#96;&#96;&#96;<br>race</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">  方法</span><br><span class="line"></span><br><span class="line">  - 接收一个Promise数组，数组中如有非Promise项，则此项当做成功</span><br><span class="line">  - 哪个Promise最快得到结果，就返回那个结果，无论成功失败</span><br><span class="line"></span><br><span class="line">```js</span><br><span class="line">function fn(time, isResolve) &#123;</span><br><span class="line">  return new Promise((resolve, reject) =&gt; &#123;</span><br><span class="line">    setTimeout(() =&gt; &#123;</span><br><span class="line">      isResolve ? resolve(`$&#123;time&#125;毫秒后我成功啦！！！`) : reject(`$&#123;time&#125;毫秒后我失败啦！！！`)</span><br><span class="line">    &#125;, time)</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Promise.race([fn(2000, true), fn(3000), fn(1000)]).then(res =&gt; &#123;</span><br><span class="line">  console.log(res)</span><br><span class="line">&#125;, err =&gt; &#123;</span><br><span class="line">  console.log(err) // 1秒后输出</span><br><span class="line">&#125;)</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="16、class"><a href="#16、class" class="headerlink" title="16、class"></a>16、class</h3><p>以前咱们使用构造函数生成对象，这么做</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">Person</span>(<span class="params">name</span>) &#123;</span><br><span class="line">  <span class="variable language_">this</span>.<span class="property">name</span> = name</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="title class_">Person</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">sayName</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="variable language_">this</span>.<span class="property">name</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> kobe = <span class="keyword">new</span> <span class="title class_">Person</span>(<span class="string">&#x27;科比&#x27;</span>)</span><br><span class="line">kobe.<span class="title function_">sayName</span>() <span class="comment">// 科比</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>而有了ES6的<code>class</code>可以这么做</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Person</span> &#123;</span><br><span class="line">  <span class="title function_">constructor</span>(<span class="params">name</span>) &#123;</span><br><span class="line">    <span class="comment">// 构造器</span></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">name</span> = name</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="title function_">sayName</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="variable language_">this</span>.<span class="property">name</span>)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> kobe = <span class="keyword">new</span> <span class="title class_">Person</span>(<span class="string">&#x27;科比&#x27;</span>)</span><br><span class="line">kobe.<span class="title function_">sayName</span>() <span class="comment">// 科比</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>值得一提的是，<code>class</code>本质也是<code>function</code>，<code>class</code>是<code>function</code>的<code>语法糖</code></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Person</span> &#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> <span class="title class_">Person</span>) <span class="comment">// function</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>除了以上，还需要知道class的以下知识点</p>
<p>静态属性和静态方法，使用<code>static</code>定义的属性和方法只能class自己用，实例用不了</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Person</span> &#123;</span><br><span class="line">  <span class="title function_">constructor</span>(<span class="params">name</span>) &#123;</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">name</span> = name</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">static</span> age = <span class="number">22</span></span><br><span class="line"></span><br><span class="line">  <span class="keyword">static</span> <span class="title function_">fn</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;哈哈&#x27;</span>)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Person</span>.<span class="property">age</span>) <span class="comment">// 22</span></span><br><span class="line"><span class="title class_">Person</span>.<span class="title function_">fn</span>() <span class="comment">// 哈哈</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> sunshine_lin = <span class="keyword">new</span> <span class="title class_">Person</span>(<span class="string">&#x27;林三心&#x27;</span>)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(sunshine_lin.<span class="property">age</span>) <span class="comment">// undefined</span></span><br><span class="line">sunshine_lin.<span class="title function_">fn</span>() <span class="comment">// fn is not a function</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><code>extend</code>继承</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Animal</span> &#123;</span><br><span class="line">  <span class="title function_">constructor</span>(<span class="params">name, age</span>) &#123;</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">name</span> = name</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">age</span> = age</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Cat</span> <span class="keyword">extends</span> <span class="title class_ inherited__">Animal</span> &#123;</span><br><span class="line">  <span class="title function_">say</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="variable language_">this</span>.<span class="property">name</span>, <span class="variable language_">this</span>.<span class="property">age</span>)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> cat = <span class="keyword">new</span> <span class="title class_">Cat</span>(<span class="string">&#x27;ketty&#x27;</span>, <span class="number">5</span>) <span class="comment">// 继承了Animal的构造器</span></span><br><span class="line">cat.<span class="title function_">say</span>() <span class="comment">// ketty 5</span></span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="17、解构赋值"><a href="#17、解构赋值" class="headerlink" title="17、解构赋值"></a>17、解构赋值</h3><p>以前想提取对象里的属性需要这么做</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> obj = &#123;</span><br><span class="line">  <span class="attr">name</span>: <span class="string">&#x27;林三心&#x27;</span>,</span><br><span class="line">  <span class="attr">age</span>: <span class="number">22</span>,</span><br><span class="line">  <span class="attr">gender</span>: <span class="string">&#x27;男&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> name = obj.<span class="property">name</span></span><br><span class="line"><span class="keyword">const</span> age = obj.<span class="property">age</span></span><br><span class="line"><span class="keyword">const</span> gender = obj.<span class="property">gender</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(name, age, gender) <span class="comment">// 林三心 22 男</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>ES6新增了解构赋值的语法</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> obj = &#123;</span><br><span class="line">  <span class="attr">name</span>: <span class="string">&#x27;林三心&#x27;</span>,</span><br><span class="line">  <span class="attr">age</span>: <span class="number">22</span>,</span><br><span class="line">  <span class="attr">gender</span>: <span class="string">&#x27;男&#x27;</span>,</span><br><span class="line">  <span class="attr">doing</span>: &#123;</span><br><span class="line">    <span class="attr">morning</span>: <span class="string">&#x27;摸鱼&#x27;</span>,</span><br><span class="line">    <span class="attr">afternoon</span>: <span class="string">&#x27;摸鱼&#x27;</span>,</span><br><span class="line">    <span class="attr">evening</span>: <span class="string">&#x27;sleep&#x27;</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> &#123; name, age, gender &#125; = obj</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(name, age, gender) <span class="comment">// 林三心 22 男</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 解构重名</span></span><br><span class="line"><span class="keyword">const</span> &#123; <span class="attr">name</span>: myname &#125; = obj</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(myname) <span class="comment">// 林三心</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 嵌套解构</span></span><br><span class="line"><span class="keyword">const</span> &#123; <span class="attr">doing</span>: &#123; evening &#125; &#125; = obj</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(evening) <span class="comment">// sleep</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>也可以进行数组的解构</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> [a, b, c] = arr</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(a, b, c) <span class="comment">// 1 2 3</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 默认赋值</span></span><br><span class="line"><span class="keyword">const</span> [a, b, c, d = <span class="number">5</span>] = arr</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(a, b, c, d) <span class="comment">// 1 2 3 5</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 乱序解构</span></span><br><span class="line"><span class="keyword">const</span> &#123; <span class="number">1</span>: a, <span class="number">0</span>: b, <span class="number">2</span>: c &#125; = arr</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(a, b, c) <span class="comment">// 2 1 3</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="18、find-和-findIndex"><a href="#18、find-和-findIndex" class="headerlink" title="18、find 和 findIndex"></a>18、find 和 findIndex</h3><ul>
<li>find：找到返回被找元素，找不到返回undefined</li>
<li>findIndex：找到返回被找元素索引，找不到返回-1</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">const findArr = [</span><br><span class="line">  &#123; name: &#x27;科比&#x27;, no: &#x27;24&#x27; &#125;,</span><br><span class="line">  &#123; name: &#x27;罗斯&#x27;, no: &#x27;1&#x27; &#125;,</span><br><span class="line">  &#123; name: &#x27;利拉德&#x27;, no: &#x27;0&#x27; &#125;</span><br><span class="line">]</span><br><span class="line"></span><br><span class="line">const kobe = findArr.find((&#123; name &#125;) =&gt; name === &#x27;科比&#x27;)</span><br><span class="line">const kobeIndex = findArr.findIndex((&#123; name &#125;) =&gt; name === &#x27;科比&#x27;)</span><br><span class="line">console.log(kobe) // &#123; name: &#x27;科比&#x27;, no: &#x27;24&#x27; &#125;</span><br><span class="line">console.log(kobeIndex) // 0</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="19、for-of-和-for-in"><a href="#19、for-of-和-for-in" class="headerlink" title="19、for of 和 for in"></a>19、for of 和 for in</h3><ul>
<li>for in ：遍历方法，可遍历对象和数组</li>
<li>for of ：遍历方法，只能遍历数组，不能遍历非iterable对象</li>
</ul>
<p>先看for in：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> obj = &#123; <span class="attr">name</span>: <span class="string">&#x27;林三心&#x27;</span>, <span class="attr">age</span>: <span class="number">22</span>, <span class="attr">gender</span>: <span class="string">&#x27;男&#x27;</span> &#125;</span><br><span class="line"><span class="keyword">const</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">let</span> key <span class="keyword">in</span> obj) &#123;</span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(key)</span><br><span class="line">&#125;</span><br><span class="line">name</span><br><span class="line">age</span><br><span class="line">gender</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">let</span> index <span class="keyword">in</span> arr) &#123;</span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(index)</span><br><span class="line">&#125;</span><br><span class="line"><span class="number">0</span> <span class="number">1</span> <span class="number">2</span> <span class="number">3</span> <span class="number">4</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>再看 for of：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span>(<span class="keyword">let</span> item <span class="keyword">of</span> arr) &#123;</span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(item)</span><br><span class="line">&#125;</span><br><span class="line"><span class="number">1</span> <span class="number">2</span> <span class="number">3</span> <span class="number">4</span> <span class="number">5</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="20、Set-和-Map"><a href="#20、Set-和-Map" class="headerlink" title="20、Set 和 Map"></a>20、Set 和 Map</h3><ul>
<li>Set</li>
</ul>
<p>先说说<code>Set</code>的基本用法</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 可不传数组</span></span><br><span class="line"><span class="keyword">const</span> set1 = <span class="keyword">new</span> <span class="title class_">Set</span>()</span><br><span class="line">set1.<span class="title function_">add</span>(<span class="number">1</span>)</span><br><span class="line">set1.<span class="title function_">add</span>(<span class="number">2</span>)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(set1) <span class="comment">// Set(2) &#123; 1, 2 &#125;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 也可传数组</span></span><br><span class="line"><span class="keyword">const</span> set2 = <span class="keyword">new</span> <span class="title class_">Set</span>([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>])</span><br><span class="line"><span class="comment">// 增加元素 使用 add</span></span><br><span class="line">set2.<span class="title function_">add</span>(<span class="number">4</span>)</span><br><span class="line">set2.<span class="title function_">add</span>(<span class="string">&#x27;林三心&#x27;</span>)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(set2) <span class="comment">// Set(5) &#123; 1, 2, 3, 4, &#x27;林三心&#x27; &#125;</span></span><br><span class="line"><span class="comment">// 是否含有某个元素 使用 has</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(set2.<span class="title function_">has</span>(<span class="number">2</span>)) <span class="comment">// true</span></span><br><span class="line"><span class="comment">// 查看长度 使用 size</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(set2.<span class="property">size</span>) <span class="comment">// 5</span></span><br><span class="line"><span class="comment">// 删除元素 使用 delete</span></span><br><span class="line">set2.<span class="title function_">delete</span>(<span class="number">2</span>)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(set2) <span class="comment">// Set(4) &#123; 1, 3, 4, &#x27;林三心&#x27; &#125;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>再说说<code>Set</code>的不重复性</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 增加一个已有元素，则增加无效，会被自动去重</span></span><br><span class="line"><span class="keyword">const</span> set1 = <span class="keyword">new</span> <span class="title class_">Set</span>([<span class="number">1</span>])</span><br><span class="line">set1.<span class="title function_">add</span>(<span class="number">1</span>)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(set1) <span class="comment">// Set(1) &#123; 1 &#125;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 传入的数组中有重复项，会自动去重</span></span><br><span class="line"><span class="keyword">const</span> set2 = <span class="keyword">new</span> <span class="title class_">Set</span>([<span class="number">1</span>, <span class="number">2</span>, <span class="string">&#x27;林三心&#x27;</span>, <span class="number">3</span>, <span class="number">3</span>, <span class="string">&#x27;林三心&#x27;</span>])</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(set2) <span class="comment">// Set(4) &#123; 1, 2, &#x27;林三心&#x27;, 3 &#125;</span></span><br><span class="line"></span><br><span class="line"><span class="title class_">Set</span><span class="string">`的不重复性中，要注意`</span>引用数据类型和<span class="title class_">NaN</span></span><br><span class="line"><span class="comment">// 两个对象都是不用的指针，所以没法去重</span></span><br><span class="line"><span class="keyword">const</span> set1 = <span class="keyword">new</span> <span class="title class_">Set</span>([<span class="number">1</span>, &#123;<span class="attr">name</span>: <span class="string">&#x27;林三心&#x27;</span>&#125;, <span class="number">2</span>, &#123;<span class="attr">name</span>: <span class="string">&#x27;林三心&#x27;</span>&#125;])</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(set1) <span class="comment">// Set(4) &#123; 1, &#123; name: &#x27;林三心&#x27; &#125;, 2, &#123; name: &#x27;林三心&#x27; &#125; &#125;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 如果是两个对象是同一指针，则能去重</span></span><br><span class="line"><span class="keyword">const</span> obj = &#123;<span class="attr">name</span>: <span class="string">&#x27;林三心&#x27;</span>&#125;</span><br><span class="line"><span class="keyword">const</span> set2 = <span class="keyword">new</span> <span class="title class_">Set</span>([<span class="number">1</span>, obj, <span class="number">2</span>, obj])</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(set2) <span class="comment">// Set(3) &#123; 1, &#123; name: &#x27;林三心&#x27; &#125;, 2 &#125;</span></span><br><span class="line"></span><br><span class="line">咱们都知道 <span class="title class_">NaN</span> !== <span class="title class_">NaN</span>，<span class="title class_">NaN</span>是自身不等于自身的，但是在<span class="title class_">Set</span>中他还是会被去重</span><br><span class="line"><span class="keyword">const</span> set = <span class="keyword">new</span> <span class="title class_">Set</span>([<span class="number">1</span>, <span class="title class_">NaN</span>, <span class="number">1</span>, <span class="title class_">NaN</span>])</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(set) <span class="comment">// Set(2) &#123; 1, NaN &#125;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>利用Set的不重复性，可以实现数组去重</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">5</span>, <span class="number">66</span>, <span class="number">9</span>, <span class="number">1</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment">// Set可利用扩展运算符转为数组哦</span></span><br><span class="line"><span class="keyword">const</span> quchongArr = [...<span class="keyword">new</span> <span class="title class_">Set</span>(arr)]</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(quchongArr) <span class="comment">// [1,  2, 3, 4, 5, 66, 9]</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<ul>
<li>Map</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">Map`对比`object`最大的好处就是，key不受`类型限制</span><br><span class="line">// 定义map</span><br><span class="line">const map1 = new Map()</span><br><span class="line">// 新增键值对 使用 set(key, value)</span><br><span class="line">map1.set(true, 1)</span><br><span class="line">map1.set(1, 2)</span><br><span class="line">map1.set(&#x27;哈哈&#x27;, &#x27;嘻嘻嘻&#x27;)</span><br><span class="line">console.log(map1) // Map(3) &#123; true =&gt; 1, 1 =&gt; 2, &#x27;哈哈&#x27; =&gt; &#x27;嘻嘻嘻&#x27; &#125;</span><br><span class="line">// 判断map是否含有某个key 使用 has(key)</span><br><span class="line">console.log(map1.has(&#x27;哈哈&#x27;)) // true</span><br><span class="line">// 获取map中某个key对应的value 使用 get(key)</span><br><span class="line">console.log(map1.get(true)) // 2</span><br><span class="line">// 删除map中某个键值对 使用 delete(key)</span><br><span class="line">map1.delete(&#x27;哈哈&#x27;)</span><br><span class="line">console.log(map1) // Map(2) &#123; true =&gt; 1, 1 =&gt; 2 &#125;</span><br><span class="line"></span><br><span class="line">// 定义map，也可传入键值对数组集合</span><br><span class="line">const map2 = new Map([[true, 1], [1, 2], [&#x27;哈哈&#x27;, &#x27;嘻嘻嘻&#x27;]])</span><br><span class="line">console.log(map2) // Map(3) &#123; true =&gt; 1, 1 =&gt; 2, &#x27;哈哈&#x27; =&gt; &#x27;嘻嘻嘻&#x27; &#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="ES7"><a href="#ES7" class="headerlink" title="ES7"></a>ES7</h2><h3 id="21、includes"><a href="#21、includes" class="headerlink" title="21、includes"></a>21、includes</h3><p>传入元素，如果数组中能找到此元素，则返回true，否则返回false</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> includeArr = [<span class="number">1</span>, <span class="number">2</span> , <span class="number">3</span>, <span class="string">&#x27;林三心&#x27;</span>, <span class="string">&#x27;科比&#x27;</span>]</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> isKobe = includeArr.<span class="title function_">includes</span>(<span class="string">&#x27;科比&#x27;</span>)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(isKobe) <span class="comment">// true</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>跟indexOf很像，但还是有区别的</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="title class_">NaN</span>]</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(arr.<span class="title function_">indexOf</span>(<span class="title class_">NaN</span>)) <span class="comment">// -1  indexOf找不到NaN</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(arr.<span class="title function_">includes</span>(<span class="title class_">NaN</span>)) <span class="comment">// true includes能找到NaN</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="22、求幂运算符"><a href="#22、求幂运算符" class="headerlink" title="22、求幂运算符"></a>22、求幂运算符</h3><p>以前求幂，我们需要这么写</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> num = <span class="title class_">Math</span>.<span class="title function_">pow</span>(<span class="number">3</span>, <span class="number">2</span>) <span class="comment">// 9</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>ES7提供了求幂运算符：<code>**</code></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> num = <span class="number">3</span> ** <span class="number">2</span> <span class="comment">// 9</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="ES8"><a href="#ES8" class="headerlink" title="ES8"></a>ES8</h2><h3 id="23、Object-values"><a href="#23、Object-values" class="headerlink" title="23、Object.values"></a>23、Object.values</h3><p>可以用来获取对象的value的集合</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> obj = &#123;</span><br><span class="line">  <span class="attr">name</span>: <span class="string">&#x27;林三心&#x27;</span>,</span><br><span class="line">  <span class="attr">age</span>: <span class="number">22</span>,</span><br><span class="line">  <span class="attr">gender</span>: <span class="string">&#x27;男&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> values = <span class="title class_">Object</span>.<span class="title function_">values</span>(obj)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(values) <span class="comment">// [ &#x27;林三心&#x27;, 22, &#x27;男&#x27; ]</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="24、Object-entries"><a href="#24、Object-entries" class="headerlink" title="24、Object.entries"></a>24、Object.entries</h3><p>可以用来获取对象的键值对集合</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> obj = &#123;</span><br><span class="line">  <span class="attr">name</span>: <span class="string">&#x27;林三心&#x27;</span>,</span><br><span class="line">  <span class="attr">age</span>: <span class="number">22</span>,</span><br><span class="line">  <span class="attr">gender</span>: <span class="string">&#x27;男&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> entries = <span class="title class_">Object</span>.<span class="title function_">entries</span>(obj)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(entries) </span><br><span class="line"><span class="comment">// [ [ &#x27;name&#x27;, &#x27;林三心&#x27; ], [ &#x27;age&#x27;, 22 ], [ &#x27;gender&#x27;, &#x27;男&#x27; ] ]</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="25、async-x2F-await"><a href="#25、async-x2F-await" class="headerlink" title="25、async&#x2F;await"></a>25、async&#x2F;await</h3><p>这个是很常用的语法了，我的理解就是：<strong>以同步方式执行异步操作</strong></p>
<p>我们平时可能会遇到这种场景，接口一，请求到数据一，而数据一被当做请求二的参数去请求数据二，我们会用Promise这么做</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">fn</span>(<span class="params"></span>) &#123;</span><br><span class="line">  <span class="comment">// 模拟第一次请求</span></span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">      <span class="title function_">resolve</span>(<span class="number">5</span>)</span><br><span class="line">    &#125;, <span class="number">1000</span>)</span><br><span class="line">  &#125;).<span class="title function_">then</span>(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="comment">// 模拟第二次请求</span></span><br><span class="line">    <span class="keyword">new</span> <span class="title class_">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">      <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">        <span class="comment">// 拿第一次请求的数据去乘10，当做第二次请求的数据</span></span><br><span class="line">        <span class="title function_">resolve</span>(res * <span class="number">10</span>)</span><br><span class="line">      &#125;, <span class="number">2000</span>)</span><br><span class="line">    &#125;).<span class="title function_">then</span>(<span class="function"><span class="params">sres</span> =&gt;</span> &#123;</span><br><span class="line">      <span class="variable language_">console</span>.<span class="title function_">log</span>(sres)</span><br><span class="line">    &#125;)</span><br><span class="line"></span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"><span class="title function_">fn</span>() <span class="comment">// 1 + 2 = 3 3秒后输出 50</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>这样的嵌套是不美观的，如果有很多个接口，那就会嵌套很多层，此时我们可以使用async&#x2F;await来以同步方式执行异步，注意以下几点：</p>
<ul>
<li>await只能在async函数里使用</li>
<li>await后面最好接Promise，如果后面接的是普通函数则会直接执行</li>
<li>async函数返回的是一个Promise</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">fn1</span> () &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">      <span class="title function_">resolve</span>(<span class="number">5</span>)</span><br><span class="line">    &#125;, <span class="number">1000</span>)</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">fn2</span> (data) &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">      <span class="title function_">resolve</span>(data * <span class="number">10</span>)</span><br><span class="line">    &#125;, <span class="number">2000</span>)</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">async</span> <span class="keyword">function</span> <span class="title function_">req</span> () &#123;</span><br><span class="line">  <span class="comment">// 同步方式执行异步，像排队一样</span></span><br><span class="line">  <span class="keyword">const</span> data1 = <span class="keyword">await</span> <span class="title function_">fn1</span>() <span class="comment">// 等待1秒后返回数据再往下执行</span></span><br><span class="line">  <span class="keyword">const</span> data2 = <span class="keyword">await</span> <span class="title function_">fn2</span>(data1) <span class="comment">// 拿data1去请求2秒后，往下走</span></span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(data2) <span class="comment">// 总共3秒后 输出 50</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="title function_">req</span>()</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="ES9"><a href="#ES9" class="headerlink" title="ES9"></a>ES9</h2><h3 id="26、for-await-of"><a href="#26、for-await-of" class="headerlink" title="26、for await of"></a>26、for await of</h3><p>我们来看以下场景哈</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">fn</span> (time) &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">      <span class="title function_">resolve</span>(<span class="string">`<span class="subst">$&#123;time&#125;</span>毫秒后我成功啦！！！`</span>)</span><br><span class="line">    &#125;, time)</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="title function_">fn</span>(<span class="number">3000</span>).<span class="title function_">then</span>(<span class="function"><span class="params">res</span> =&gt;</span> <span class="variable language_">console</span>.<span class="title function_">log</span>(res))</span><br><span class="line"><span class="title function_">fn</span>(<span class="number">1000</span>).<span class="title function_">then</span>(<span class="function"><span class="params">res</span> =&gt;</span> <span class="variable language_">console</span>.<span class="title function_">log</span>(res))</span><br><span class="line"><span class="title function_">fn</span>(<span class="number">2000</span>).<span class="title function_">then</span>(<span class="function"><span class="params">res</span> =&gt;</span> <span class="variable language_">console</span>.<span class="title function_">log</span>(res))</span><br><span class="line"></span><br><span class="line">结果是</span><br><span class="line"></span><br><span class="line"><span class="number">1000</span>毫秒后我成功啦！！！</span><br><span class="line"><span class="number">2000</span>毫秒后我成功啦！！！</span><br><span class="line"><span class="number">3000</span>毫秒后我成功啦！！！</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>但是我想要这个结果</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">3000</span>毫秒后我成功啦！！！</span><br><span class="line"><span class="number">1000</span>毫秒后我成功啦！！！</span><br><span class="line"><span class="number">2000</span>毫秒后我成功啦！！！</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>第一时间我们肯定想到的是<code>async/await</code></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">fn</span> (time) &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">      <span class="title function_">resolve</span>(<span class="string">`<span class="subst">$&#123;time&#125;</span>毫秒后我成功啦！！！`</span>)</span><br><span class="line">    &#125;, time)</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">async</span> <span class="keyword">function</span> <span class="title function_">asyncFn</span> () &#123;</span><br><span class="line">  <span class="comment">// 排队</span></span><br><span class="line">  <span class="keyword">const</span> data1 = <span class="keyword">await</span> <span class="title function_">fn</span>(<span class="number">3000</span>)</span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(data1) <span class="comment">// 3秒后 3000毫秒后我成功啦！！！</span></span><br><span class="line">  <span class="keyword">const</span> data2 = <span class="keyword">await</span> <span class="title function_">fn</span>(<span class="number">1000</span>)</span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(data2) <span class="comment">// 再过1秒 1000毫秒后我成功啦！！！</span></span><br><span class="line">  <span class="keyword">const</span> data3 = <span class="keyword">await</span> <span class="title function_">fn</span>(<span class="number">2000</span>)</span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(data3) <span class="comment">// 再过2秒 2000毫秒后我成功啦！！！</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>但是上面代码也是有缺点的，如果有几十个，那不是得写几十个await，有没有一种方法可以通过循环来输出呢？</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">fn</span> (time) &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">      <span class="title function_">resolve</span>(<span class="string">`<span class="subst">$&#123;time&#125;</span>毫秒后我成功啦！！！`</span>)</span><br><span class="line">    &#125;, time)</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">async</span> <span class="keyword">function</span> <span class="title function_">asyncFn</span> () &#123;</span><br><span class="line">  <span class="keyword">const</span> arr = [<span class="title function_">fn</span>(<span class="number">3000</span>), <span class="title function_">fn</span>(<span class="number">1000</span>), <span class="title function_">fn</span>(<span class="number">1000</span>), <span class="title function_">fn</span>(<span class="number">2000</span>), <span class="title function_">fn</span>(<span class="number">500</span>)]</span><br><span class="line">  <span class="keyword">for</span> <span class="keyword">await</span> (<span class="keyword">let</span> x <span class="keyword">of</span> arr) &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(x)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="title function_">asyncFn</span>()</span><br><span class="line"><span class="number">3000</span>毫秒后我成功啦！！！</span><br><span class="line"><span class="number">1000</span>毫秒后我成功啦！！！</span><br><span class="line"><span class="number">1000</span>毫秒后我成功啦！！！</span><br><span class="line"><span class="number">2000</span>毫秒后我成功啦！！！</span><br><span class="line"><span class="number">500</span>毫秒后我成功啦！！！</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="27、Promise-finally"><a href="#27、Promise-finally" class="headerlink" title="27、Promise.finally"></a>27、Promise.finally</h3><p>新增的Promise方法，无论失败或者成功状态，都会执行这个函数</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// cheng</span></span><br><span class="line"><span class="keyword">new</span> <span class="title class_">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="title function_">resolve</span>(<span class="string">&#x27;成功喽&#x27;</span>)</span><br><span class="line">&#125;).<span class="title function_">then</span>(</span><br><span class="line">  <span class="function"><span class="params">res</span> =&gt;</span> &#123; <span class="variable language_">console</span>.<span class="title function_">log</span>(res) &#125;,</span><br><span class="line">  <span class="function"><span class="params">err</span> =&gt;</span> &#123; <span class="variable language_">console</span>.<span class="title function_">log</span>(err) &#125;</span><br><span class="line">).<span class="title function_">finally</span>(<span class="function">() =&gt;</span> &#123; <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;我是finally&#x27;</span>) &#125;)</span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> <span class="title class_">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="title function_">reject</span>(<span class="string">&#x27;失败喽&#x27;</span>)</span><br><span class="line">&#125;).<span class="title function_">then</span>(</span><br><span class="line">  <span class="function"><span class="params">res</span> =&gt;</span> &#123; <span class="variable language_">console</span>.<span class="title function_">log</span>(res) &#125;,</span><br><span class="line">  <span class="function"><span class="params">err</span> =&gt;</span> &#123; <span class="variable language_">console</span>.<span class="title function_">log</span>(err) &#125;</span><br><span class="line">).<span class="title function_">finally</span>(<span class="function">() =&gt;</span> &#123; <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;我是finally&#x27;</span>) &#125;)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="ES10"><a href="#ES10" class="headerlink" title="ES10"></a>ES10</h2><h3 id="28、Array-flat"><a href="#28、Array-flat" class="headerlink" title="28、Array.flat"></a>28、Array.flat</h3><p>有一个二维数组，我想让他变成一维数组：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, [<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]]</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(arr.<span class="title function_">flat</span>()) <span class="comment">// [ 1, 2, 3, 4, 5, 6 ]</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>还可以传参数，参数为降维的次数</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, [<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, [<span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>]]]</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(arr.<span class="title function_">flat</span>(<span class="number">2</span>))</span><br><span class="line">[</span><br><span class="line">  <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>,</span><br><span class="line">  <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span></span><br><span class="line">]</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>如果传的是一个无限大的数字，那么就实现了多维数组(无论几维)降为一维数组</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, [<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, [<span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>, [<span class="number">10</span>, <span class="number">11</span>, <span class="number">12</span>]]]]</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(arr.<span class="title function_">flat</span>(<span class="title class_">Infinity</span>))</span><br><span class="line">[</span><br><span class="line">   <span class="number">1</span>,  <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>,  <span class="number">5</span>,</span><br><span class="line">   <span class="number">6</span>,  <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>, <span class="number">10</span>,</span><br><span class="line">   <span class="number">11</span>, <span class="number">12</span></span><br><span class="line">]</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="29、Array-flatMap"><a href="#29、Array-flatMap" class="headerlink" title="29、Array.flatMap"></a>29、Array.flatMap</h3><p>现在给你一个需求</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr = [<span class="string">&quot;科比 詹姆斯 安东尼&quot;</span>, <span class="string">&quot;利拉德 罗斯 麦科勒姆&quot;</span>];</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>将上面数组转为</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[ <span class="string">&#x27;科比&#x27;</span>, <span class="string">&#x27;詹姆斯&#x27;</span>, <span class="string">&#x27;安东尼&#x27;</span>, <span class="string">&#x27;利拉德&#x27;</span>, <span class="string">&#x27;罗斯&#x27;</span>, <span class="string">&#x27;麦科勒姆&#x27;</span> ]</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>第一时间想到<code>map + flat</code></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(arr.<span class="title function_">map</span>(<span class="function"><span class="params">x</span> =&gt;</span> x.<span class="title function_">split</span>(<span class="string">&quot; &quot;</span>)).<span class="title function_">flat</span>());</span><br><span class="line"><span class="comment">// [ &#x27;科比&#x27;, &#x27;詹姆斯&#x27;, &#x27;安东尼&#x27;, &#x27;利拉德&#x27;, &#x27;罗斯&#x27;, &#x27;麦科勒姆&#x27; ]</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><code>flatMap</code>就是<code>flat + map</code>，一个方法顶两个</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(arr.<span class="title function_">flatMap</span>(<span class="function"><span class="params">x</span> =&gt;</span> x.<span class="title function_">split</span>(<span class="string">&quot; &quot;</span>)));</span><br><span class="line"><span class="comment">// [ &#x27;科比&#x27;, &#x27;詹姆斯&#x27;, &#x27;安东尼&#x27;, &#x27;利拉德&#x27;, &#x27;罗斯&#x27;, &#x27;麦科勒姆&#x27; ]</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="30、BigInt"><a href="#30、BigInt" class="headerlink" title="30、BigInt"></a>30、BigInt</h3><p><code>BigInt</code>是ES10新加的一种JavaScript数据类型，用来表示表示大于 <code>2^53 - 1</code> 的整数，<code>2^53 - 1</code>是ES10之前，JavaScript所能表示最大的数字</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> theBiggestInt = <span class="number">9007199254740991n</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> alsoHuge = <span class="title class_">BigInt</span>(<span class="number">9007199254740991</span>);</span><br><span class="line"><span class="comment">// 9007199254740991n</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> hugeString = <span class="title class_">BigInt</span>(<span class="string">&quot;9007199254740991&quot;</span>);</span><br><span class="line"><span class="comment">// 9007199254740991n</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> hugeHex = <span class="title class_">BigInt</span>(<span class="string">&quot;0x1fffffffffffff&quot;</span>);</span><br><span class="line"><span class="comment">// 9007199254740991n</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> hugeBin = <span class="title class_">BigInt</span>(<span class="string">&quot;0b11111111111111111111111111111111111111111111111111111&quot;</span>);</span><br><span class="line"><span class="comment">// 9007199254740991n</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>哦对了，既然是JavaScript新的数据类型，那他的<code>typeof</code>是啥？</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> bigNum = <span class="title class_">BigInt</span>(<span class="number">1728371927189372189739217</span>)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> bigNum) <span class="comment">// bigint</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>所以以后面试官问你JavaScript有多少种数据类型，别傻傻答6种了，要答8种，把ES6的<code>Symbol</code>和ES10的<code>BigInt</code>也加上去</p>
<h3 id="31、Object-fromEntries"><a href="#31、Object-fromEntries" class="headerlink" title="31、Object.fromEntries"></a>31、Object.fromEntries</h3><p>前面ES8的<code>Object.entries</code>是把<code>对象转成键值对数组</code>，而<code>Object.fromEntries</code>则相反，是把<code>键值对数组转为对象</code></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr = [</span><br><span class="line">  [<span class="string">&#x27;name&#x27;</span>, <span class="string">&#x27;林三心&#x27;</span>],</span><br><span class="line">  [<span class="string">&#x27;age&#x27;</span>, <span class="number">22</span>],</span><br><span class="line">  [<span class="string">&#x27;gender&#x27;</span>, <span class="string">&#x27;男&#x27;</span>]</span><br><span class="line">]</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Object</span>.<span class="title function_">fromEntries</span>(arr)) <span class="comment">// &#123; name: &#x27;林三心&#x27;, age: 22, gender: &#x27;男&#x27; &#125;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>他还有一个用处，就是把<code>Map转为对象</code></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> map = <span class="keyword">new</span> <span class="title class_">Map</span>()</span><br><span class="line">map.<span class="title function_">set</span>(<span class="string">&#x27;name&#x27;</span>, <span class="string">&#x27;林三心&#x27;</span>)</span><br><span class="line">map.<span class="title function_">set</span>(<span class="string">&#x27;age&#x27;</span>, <span class="number">22</span>)</span><br><span class="line">map.<span class="title function_">set</span>(<span class="string">&#x27;gender&#x27;</span>, <span class="string">&#x27;男&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(map) <span class="comment">// Map(3) &#123; &#x27;name&#x27; =&gt; &#x27;林三心&#x27;, &#x27;age&#x27; =&gt; 22, &#x27;gender&#x27; =&gt; &#x27;男&#x27; &#125;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> obj = <span class="title class_">Object</span>.<span class="title function_">fromEntries</span>(map)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(obj) <span class="comment">// &#123; name: &#x27;林三心&#x27;, age: 22, gender: &#x27;男&#x27; &#125;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="32、String-trimStart-amp-amp-String-trimEnd"><a href="#32、String-trimStart-amp-amp-String-trimEnd" class="headerlink" title="32、String.trimStart &amp;&amp; String.trimEnd"></a>32、String.trimStart &amp;&amp; String.trimEnd</h3><p>咱们都知道JavaScript有个trim方法，可以清除字符串首尾的空格</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> str = <span class="string">&#x27;    林三心    &#x27;</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str.<span class="title function_">trim</span>()) <span class="comment">// &#x27;林三心&#x27;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>trimStart和trimEnd用来单独去除字符串的首和尾的空格</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> str = <span class="string">&#x27;    林三心    &#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 去除首部空格</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str.<span class="title function_">trimStart</span>()) <span class="comment">// &#x27;林三心   &#x27;</span></span><br><span class="line"><span class="comment">// 去除尾部空格</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str.<span class="title function_">trimEnd</span>()) <span class="comment">// &#x27;   林三心&#x27;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="ES11"><a href="#ES11" class="headerlink" title="ES11"></a>ES11</h2><h3 id="33、Promise-allSettled"><a href="#33、Promise-allSettled" class="headerlink" title="33、Promise.allSettled"></a>33、Promise.allSettled</h3><p>ES11新增的Promise的方法</p>
<ul>
<li>接收一个Promise数组，数组中如有非Promise项，则此项当做成功</li>
<li>把每一个Promise的结果，集合成数组，返回</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">fn</span>(<span class="params">time, isResolve</span>) &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">      isResolve ? <span class="title function_">resolve</span>(<span class="string">`<span class="subst">$&#123;time&#125;</span>毫秒后我成功啦！！！`</span>) : <span class="title function_">reject</span>(<span class="string">`<span class="subst">$&#123;time&#125;</span>毫秒后我失败啦！！！`</span>)</span><br><span class="line">    &#125;, time)</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="title class_">Promise</span>.<span class="title function_">allSettled</span>([<span class="title function_">fn</span>(<span class="number">2000</span>, <span class="literal">true</span>), <span class="title function_">fn</span>(<span class="number">3000</span>), <span class="title function_">fn</span>(<span class="number">1000</span>)]).<span class="title function_">then</span>(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(res)</span><br><span class="line">  <span class="comment">// 3秒后输出 </span></span><br><span class="line">  [</span><br><span class="line">  &#123; <span class="attr">status</span>: <span class="string">&#x27;fulfilled&#x27;</span>, <span class="attr">value</span>: <span class="string">&#x27;2000毫秒后我成功啦！！！&#x27;</span> &#125;,</span><br><span class="line">  &#123; <span class="attr">status</span>: <span class="string">&#x27;rejected&#x27;</span>, <span class="attr">reason</span>: <span class="string">&#x27;3000毫秒后我失败啦！！！&#x27;</span> &#125;,</span><br><span class="line">  &#123; <span class="attr">status</span>: <span class="string">&#x27;rejected&#x27;</span>, <span class="attr">reason</span>: <span class="string">&#x27;1000毫秒后我失败啦！！！&#x27;</span> &#125;</span><br><span class="line">]</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="34、-和"><a href="#34、-和" class="headerlink" title="34、?. 和 ??"></a>34、?. 和 ??</h3><ul>
<li>先说说<code>?.</code>，中文名为<code>可选链</code></li>
</ul>
<p>比如我们需要一个变量，是数组且有长度，才做某些操作</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> list = <span class="literal">null</span></span><br><span class="line"><span class="comment">// do something</span></span><br><span class="line"><span class="keyword">if</span> (list &amp;&amp; list.<span class="property">length</span>) &#123;</span><br><span class="line">  <span class="comment">// do something</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 使用可选链</span></span><br><span class="line"><span class="keyword">const</span> list = <span class="literal">null</span></span><br><span class="line"><span class="comment">// do something</span></span><br><span class="line"><span class="keyword">if</span> (list?.<span class="property">length</span>) &#123;</span><br><span class="line">  <span class="comment">// do something</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>比如有一个对象，我要取一个可能不存在的值，甚至我们都不确定obj是否存在</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> obj = &#123;</span><br><span class="line">  <span class="attr">cat</span>: &#123;</span><br><span class="line">    <span class="attr">name</span>: <span class="string">&#x27;哈哈&#x27;</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> dog = obj &amp;&amp; obj.<span class="property">dog</span> &amp;&amp; obj.<span class="property">dog</span>.<span class="property">name</span> <span class="comment">// undefined</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 可选链</span></span><br><span class="line"><span class="keyword">const</span> obj = &#123;</span><br><span class="line">  <span class="attr">cat</span>: &#123;</span><br><span class="line">    <span class="attr">name</span>: <span class="string">&#x27;哈哈&#x27;</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> dog = obj?.<span class="property">dog</span>?.<span class="property">name</span> <span class="comment">// undefined</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>比如有一个数组，我不确定它存不存在，存在的话就取索引为1的值</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr = <span class="literal">null</span></span><br><span class="line"><span class="comment">// do something</span></span><br><span class="line"><span class="keyword">const</span> item = arr &amp;&amp; arr[<span class="number">1</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment">// 可选链</span></span><br><span class="line"><span class="keyword">const</span> arr = <span class="literal">null</span></span><br><span class="line"><span class="comment">// do something</span></span><br><span class="line"><span class="keyword">const</span> item = arr?.[<span class="number">1</span>]</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>比如有一个函数，我们不确定它存不存在，存在的话就执行它</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> fn = <span class="literal">null</span></span><br><span class="line"><span class="comment">// do something</span></span><br><span class="line"><span class="keyword">const</span> res = fn &amp;&amp; <span class="title function_">fn</span>()</span><br><span class="line"></span><br><span class="line"><span class="comment">// 可选链</span></span><br><span class="line"><span class="keyword">const</span> fn = <span class="literal">null</span></span><br><span class="line"><span class="comment">// do something</span></span><br><span class="line"><span class="keyword">const</span> res = fn?.()</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<ul>
<li>再说说<code>??</code>，中文名为<code>空位合并运算符</code></li>
</ul>
<p>请看以下代码，咱们使用<code>||</code>运算符，只要左边是<code>假值</code>，就会返回右边的数据</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> a = <span class="number">0</span> || <span class="string">&#x27;林三心&#x27;</span> <span class="comment">// 林三心</span></span><br><span class="line"><span class="keyword">const</span> b = <span class="string">&#x27;&#x27;</span> || <span class="string">&#x27;林三心&#x27;</span> <span class="comment">// 林三心</span></span><br><span class="line"><span class="keyword">const</span> c = <span class="literal">false</span> || <span class="string">&#x27;林三心&#x27;</span> <span class="comment">// 林三心</span></span><br><span class="line"><span class="keyword">const</span> d = <span class="literal">undefined</span> || <span class="string">&#x27;林三心&#x27;</span> <span class="comment">// 林三心</span></span><br><span class="line"><span class="keyword">const</span> e = <span class="literal">null</span> || <span class="string">&#x27;林三心&#x27;</span> <span class="comment">// 林三心</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>而<code>??</code>和<code>||</code>最大的区别是，在<code>??</code>这，只有<code>undefined和null</code>才算假值</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> a = <span class="number">0</span> ?? <span class="string">&#x27;林三心&#x27;</span> <span class="comment">// 0</span></span><br><span class="line"><span class="keyword">const</span> b = <span class="string">&#x27;&#x27;</span> ?? <span class="string">&#x27;林三心&#x27;</span> <span class="comment">// &#x27;&#x27;</span></span><br><span class="line"><span class="keyword">const</span> c = <span class="literal">false</span> ?? <span class="string">&#x27;林三心&#x27;</span> <span class="comment">// false</span></span><br><span class="line"><span class="keyword">const</span> d = <span class="literal">undefined</span> ?? <span class="string">&#x27;林三心&#x27;</span> <span class="comment">// 林三心</span></span><br><span class="line"><span class="keyword">const</span> e = <span class="literal">null</span> ?? <span class="string">&#x27;林三心&#x27;</span> <span class="comment">// 林三心</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="ES12"><a href="#ES12" class="headerlink" title="ES12"></a>ES12</h2><h3 id="35、Promise-any"><a href="#35、Promise-any" class="headerlink" title="35、Promise.any"></a>35、Promise.any</h3><p>E12新增的Promise的方法</p>
<ul>
<li>接收一个Promise数组，数组中如有非Promise项，则此项当做成功</li>
<li>如果有一个Promise成功，则返回这个成功结果</li>
<li>如果所有Promise都失败，则报错</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 当有成功的时候，返回最快那个成功</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">fn</span>(<span class="params">time, isResolve</span>) &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">      isResolve ? <span class="title function_">resolve</span>(<span class="string">`<span class="subst">$&#123;time&#125;</span>毫秒后我成功啦！！！`</span>) : <span class="title function_">reject</span>(<span class="string">`<span class="subst">$&#123;time&#125;</span>毫秒后我失败啦！！！`</span>)</span><br><span class="line">    &#125;, time)</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="title class_">Promise</span>.<span class="title function_">any</span>([<span class="title function_">fn</span>(<span class="number">2000</span>, <span class="literal">true</span>), <span class="title function_">fn</span>(<span class="number">3000</span>), <span class="title function_">fn</span>(<span class="number">1000</span>, <span class="literal">true</span>)]).<span class="title function_">then</span>(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(res) <span class="comment">// 1秒后 输出  1000毫秒后我成功啦</span></span><br><span class="line">&#125;, <span class="function"><span class="params">err</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(err)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 当全都失败时</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">fn</span>(<span class="params">time, isResolve</span>) &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">      isResolve ? <span class="title function_">resolve</span>(<span class="string">`<span class="subst">$&#123;time&#125;</span>毫秒后我成功啦！！！`</span>) : <span class="title function_">reject</span>(<span class="string">`<span class="subst">$&#123;time&#125;</span>毫秒后我失败啦！！！`</span>)</span><br><span class="line">    &#125;, time)</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="title class_">Promise</span>.<span class="title function_">any</span>([<span class="title function_">fn</span>(<span class="number">2000</span>), <span class="title function_">fn</span>(<span class="number">3000</span>), <span class="title function_">fn</span>(<span class="number">1000</span>)]).<span class="title function_">then</span>(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(res)</span><br><span class="line">&#125;, <span class="function"><span class="params">err</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(err) <span class="comment">// 3秒后 报错 all Error</span></span><br><span class="line">&#125;)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="36、数字分隔符"><a href="#36、数字分隔符" class="headerlink" title="36、数字分隔符"></a>36、数字分隔符</h3><p>数字分隔符可以让你在定义长数字时，更加地一目了然</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> num = <span class="number">1000000000</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 使用数字分隔符</span></span><br><span class="line"><span class="keyword">const</span> num = <span class="number">1_000_000_000</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="37、-x3D-和-amp-amp-x3D"><a href="#37、-x3D-和-amp-amp-x3D" class="headerlink" title="37、||&#x3D; 和 &amp;&amp;&#x3D;"></a>37、||&#x3D; 和 &amp;&amp;&#x3D;</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">或等于(||=)   a ||= b 等同于 a || (a = b);</span><br><span class="line"></span><br><span class="line">且等于(&amp;&amp;=)   a &amp;&amp;= b 等同于 a &amp;&amp; (a = b);</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="不知道是ES几"><a href="#不知道是ES几" class="headerlink" title="不知道是ES几"></a>不知道是ES几</h2><h3 id="38、对象计算属性"><a href="#38、对象计算属性" class="headerlink" title="38、对象计算属性"></a>38、对象计算属性</h3><p>我们经常碰到这样的问题，无论是在微信小程序还是React中，我们需要根据某个条件去修改某个数据</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (type === <span class="string">&#x27;boy&#x27;</span>) &#123;</span><br><span class="line">  <span class="variable language_">this</span>.<span class="title function_">setData</span>(&#123;</span><br><span class="line">    <span class="attr">boyName</span>: name</span><br><span class="line">  &#125;)</span><br><span class="line">&#125; <span class="keyword">else</span> <span class="keyword">if</span> (type === <span class="string">&#x27;girl&#x27;</span>) &#123;</span><br><span class="line">  <span class="variable language_">this</span>.<span class="title function_">setData</span>(&#123;</span><br><span class="line">    <span class="attr">girlName</span>: name</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>我也不知道这个新特性叫啥，我就自己取名叫<code>属性动态属性</code>哈哈哈</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable language_">this</span>.<span class="title function_">setData</span>(&#123;</span><br><span class="line">  [<span class="string">`<span class="subst">$&#123;type&#125;</span>Name`</span>]: name</span><br><span class="line">&#125;)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<hr>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">Author: </span><span class="post-copyright-info"><a href="https://szhihao.gitee.io">孙志豪</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">Link: </span><span class="post-copyright-info"><a href="https://szhihao.gitee.io/2022/10/11/06-%E6%A0%B8%E5%BF%83js%EF%BC%88%E9%AA%9A%E5%8C%85%E6%8A%80%E5%B7%A7%E7%AF%87%EF%BC%89/%E6%80%BB%E7%BB%93%E4%BA%8638%E4%B8%AAES6-ES12%E7%9A%84%E5%BC%80%E5%8F%91%E6%8A%80%E5%B7%A7/">https://szhihao.gitee.io/2022/10/11/06-%E6%A0%B8%E5%BF%83js%EF%BC%88%E9%AA%9A%E5%8C%85%E6%8A%80%E5%B7%A7%E7%AF%87%EF%BC%89/%E6%80%BB%E7%BB%93%E4%BA%8638%E4%B8%AAES6-ES12%E7%9A%84%E5%BC%80%E5%8F%91%E6%8A%80%E5%B7%A7/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">Copyright Notice: </span><span class="post-copyright-info">All articles in this blog are licensed under <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> unless stating additionally.</span></div></div><div class="tag_share"><div class="post-meta__tag-list"></div><div class="post_share"><div class="social-share" data-image="/img/%E5%A4%A9%E7%A9%BA.jpeg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2022/10/11/09-%E6%A0%B8%E5%BF%83js%EF%BC%88%E5%8E%9F%E7%90%86%E7%AF%87%EF%BC%89/JS%E5%8E%9F%E5%9E%8B%E9%93%BE%E4%B8%8E%E7%BB%A7%E6%89%BF%E5%88%AB%E5%86%8D%E8%A2%AB%E9%97%AE%E5%80%92%E4%BA%86/"><img class="prev-cover" src= "" data-lazy-src="/img/%E5%A4%A9%E7%A9%BA.jpeg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">Previous Post</div><div class="prev_info">JS原型链与继承别再被问倒了</div></div></a></div><div class="next-post pull-right"><a href="/2022/10/11/10-%E5%89%8D%E7%AB%AFHTML%E7%AF%87%E7%AB%A0/%E6%8E%98%E9%87%91HTML%E5%9F%BA%E7%A1%80%E7%AF%87/"><img class="next-cover" src= "" data-lazy-src="/img/%E5%A4%A9%E7%A9%BA.jpeg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">Next Post</div><div class="next_info">前端HTML篇章</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src= "" data-lazy-src="/img/%E5%A4%B4%E5%83%8F.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">孙志豪</div><div class="author-info__description">吾日三省吾身：为人谋而不忠乎？与朋友交而不信乎？传不习乎？</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">Articles</div><div class="length-num">44</div></a><a href="/tags/"><div class="headline">Tags</div><div class="length-num">1</div></a><a href="/categories/"><div class="headline">Categories</div><div class="length-num">15</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://gitee.com/szhihao/szhihao"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://gitee.com/szhihao/szhihao" target="_blank" title="Gitee"><i class="fab fa-github"></i></a><a class="social-icon" href="https://blog.csdn.net/qq_54753561" target="_blank" title="CSDN"><i class="fa fa-book-open"></i></a><a class="social-icon" href="mailto:zhihaomail@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>Announcement</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>Catalog</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%80%BB%E7%BB%93%E4%BA%8638%E4%B8%AAES6-ES12%E7%9A%84%E5%BC%80%E5%8F%91%E6%8A%80%E5%B7%A7"><span class="toc-number">1.</span> <span class="toc-text">总结了38个ES6-ES12的开发技巧</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#ES6"><span class="toc-number">1.1.</span> <span class="toc-text">ES6</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81let-%E5%92%8C-const"><span class="toc-number">1.1.1.</span> <span class="toc-text">1、let 和 const</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%E3%80%81%E9%BB%98%E8%AE%A4%E5%8F%82%E6%95%B0"><span class="toc-number">1.1.2.</span> <span class="toc-text">2、默认参数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3%E3%80%81%E6%89%A9%E5%B1%95%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-number">1.1.3.</span> <span class="toc-text">3、扩展运算符</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4%E3%80%81%E5%89%A9%E4%BD%99%E5%8F%82%E6%95%B0"><span class="toc-number">1.1.4.</span> <span class="toc-text">4、剩余参数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5%E3%80%81%E6%A8%A1%E6%9D%BF%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="toc-number">1.1.5.</span> <span class="toc-text">5、模板字符串</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6%E3%80%81Object-keys"><span class="toc-number">1.1.6.</span> <span class="toc-text">6、Object.keys</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#7%E3%80%81%E7%AE%AD%E5%A4%B4%E5%87%BD%E6%95%B0"><span class="toc-number">1.1.7.</span> <span class="toc-text">7、箭头函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#8%E3%80%81Array-prototype-forEach"><span class="toc-number">1.1.8.</span> <span class="toc-text">8、Array.prototype.forEach</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#9%E3%80%81Array-prototype-map"><span class="toc-number">1.1.9.</span> <span class="toc-text">9、Array.prototype.map</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#10%E3%80%81Array-prototype-filter"><span class="toc-number">1.1.10.</span> <span class="toc-text">10、Array.prototype.filter</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#11%E3%80%81Array-prototype-some"><span class="toc-number">1.1.11.</span> <span class="toc-text">11、Array.prototype.some</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#12%E3%80%81Array-prototype-every"><span class="toc-number">1.1.12.</span> <span class="toc-text">12、Array.prototype.every</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#13%E3%80%81Array-prototype-reduce"><span class="toc-number">1.1.13.</span> <span class="toc-text">13、Array.prototype.reduce</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#14%E3%80%81%E5%AF%B9%E8%B1%A1%E5%B1%9E%E6%80%A7%E5%90%8C%E5%90%8D%E7%AE%80%E5%86%99"><span class="toc-number">1.1.14.</span> <span class="toc-text">14、对象属性同名简写</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#15%E3%80%81Promise"><span class="toc-number">1.1.15.</span> <span class="toc-text">15、Promise</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#16%E3%80%81class"><span class="toc-number">1.1.16.</span> <span class="toc-text">16、class</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#17%E3%80%81%E8%A7%A3%E6%9E%84%E8%B5%8B%E5%80%BC"><span class="toc-number">1.1.17.</span> <span class="toc-text">17、解构赋值</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#18%E3%80%81find-%E5%92%8C-findIndex"><span class="toc-number">1.1.18.</span> <span class="toc-text">18、find 和 findIndex</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#19%E3%80%81for-of-%E5%92%8C-for-in"><span class="toc-number">1.1.19.</span> <span class="toc-text">19、for of 和 for in</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#20%E3%80%81Set-%E5%92%8C-Map"><span class="toc-number">1.1.20.</span> <span class="toc-text">20、Set 和 Map</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#ES7"><span class="toc-number">1.2.</span> <span class="toc-text">ES7</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#21%E3%80%81includes"><span class="toc-number">1.2.1.</span> <span class="toc-text">21、includes</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#22%E3%80%81%E6%B1%82%E5%B9%82%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-number">1.2.2.</span> <span class="toc-text">22、求幂运算符</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#ES8"><span class="toc-number">1.3.</span> <span class="toc-text">ES8</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#23%E3%80%81Object-values"><span class="toc-number">1.3.1.</span> <span class="toc-text">23、Object.values</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#24%E3%80%81Object-entries"><span class="toc-number">1.3.2.</span> <span class="toc-text">24、Object.entries</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#25%E3%80%81async-x2F-await"><span class="toc-number">1.3.3.</span> <span class="toc-text">25、async&#x2F;await</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#ES9"><span class="toc-number">1.4.</span> <span class="toc-text">ES9</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#26%E3%80%81for-await-of"><span class="toc-number">1.4.1.</span> <span class="toc-text">26、for await of</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#27%E3%80%81Promise-finally"><span class="toc-number">1.4.2.</span> <span class="toc-text">27、Promise.finally</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#ES10"><span class="toc-number">1.5.</span> <span class="toc-text">ES10</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#28%E3%80%81Array-flat"><span class="toc-number">1.5.1.</span> <span class="toc-text">28、Array.flat</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#29%E3%80%81Array-flatMap"><span class="toc-number">1.5.2.</span> <span class="toc-text">29、Array.flatMap</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#30%E3%80%81BigInt"><span class="toc-number">1.5.3.</span> <span class="toc-text">30、BigInt</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#31%E3%80%81Object-fromEntries"><span class="toc-number">1.5.4.</span> <span class="toc-text">31、Object.fromEntries</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#32%E3%80%81String-trimStart-amp-amp-String-trimEnd"><span class="toc-number">1.5.5.</span> <span class="toc-text">32、String.trimStart &amp;&amp; String.trimEnd</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#ES11"><span class="toc-number">1.6.</span> <span class="toc-text">ES11</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#33%E3%80%81Promise-allSettled"><span class="toc-number">1.6.1.</span> <span class="toc-text">33、Promise.allSettled</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#34%E3%80%81-%E5%92%8C"><span class="toc-number">1.6.2.</span> <span class="toc-text">34、?. 和 ??</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#ES12"><span class="toc-number">1.7.</span> <span class="toc-text">ES12</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#35%E3%80%81Promise-any"><span class="toc-number">1.7.1.</span> <span class="toc-text">35、Promise.any</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#36%E3%80%81%E6%95%B0%E5%AD%97%E5%88%86%E9%9A%94%E7%AC%A6"><span class="toc-number">1.7.2.</span> <span class="toc-text">36、数字分隔符</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#37%E3%80%81-x3D-%E5%92%8C-amp-amp-x3D"><span class="toc-number">1.7.3.</span> <span class="toc-text">37、||&#x3D; 和 &amp;&amp;&#x3D;</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%8D%E7%9F%A5%E9%81%93%E6%98%AFES%E5%87%A0"><span class="toc-number">1.8.</span> <span class="toc-text">不知道是ES几</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#38%E3%80%81%E5%AF%B9%E8%B1%A1%E8%AE%A1%E7%AE%97%E5%B1%9E%E6%80%A7"><span class="toc-number">1.8.1.</span> <span class="toc-text">38、对象计算属性</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>Recent Post</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2022/10/12/04-%E5%89%8D%E7%AB%AF%EF%BC%88%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96%E7%AF%87%EF%BC%89/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96%E7%AF%87/" title="性能优化面试题"><img src= "" data-lazy-src="/img/%E5%A4%A9%E7%A9%BA.jpeg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="性能优化面试题"/></a><div class="content"><a class="title" href="/2022/10/12/04-%E5%89%8D%E7%AB%AF%EF%BC%88%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96%E7%AF%87%EF%BC%89/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96%E7%AF%87/" title="性能优化面试题">性能优化面试题</a><time datetime="2022-10-12T12:01:36.000Z" title="Created 2022-10-12 20:01:36">2022-10-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/10/11/05-%E6%A0%B8%E5%BF%83js%EF%BC%88%E6%96%B9%E6%B3%95%E5%AE%B6%E8%A7%84%EF%BC%89/JS%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%96%B9%E6%B3%95%E2%80%94%E2%80%94%E9%80%9F%E8%AE%B0%E5%B0%8F%E6%9C%AC%E5%AD%90/" title="JS字符串方法"><img src= "" data-lazy-src="/img/%E5%A4%A9%E7%A9%BA.jpeg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="JS字符串方法"/></a><div class="content"><a class="title" href="/2022/10/11/05-%E6%A0%B8%E5%BF%83js%EF%BC%88%E6%96%B9%E6%B3%95%E5%AE%B6%E8%A7%84%EF%BC%89/JS%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%96%B9%E6%B3%95%E2%80%94%E2%80%94%E9%80%9F%E8%AE%B0%E5%B0%8F%E6%9C%AC%E5%AD%90/" title="JS字符串方法">JS字符串方法</a><time datetime="2022-10-11T12:01:36.000Z" title="Created 2022-10-11 20:01:36">2022-10-11</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/10/11/05-%E6%A0%B8%E5%BF%83js%EF%BC%88%E6%96%B9%E6%B3%95%E5%AE%B6%E8%A7%84%EF%BC%89/JS%E5%AF%B9%E8%B1%A1%E6%96%B9%E6%B3%95%E2%80%94%E2%80%94%E9%80%9F%E8%AE%B0%E5%B0%8F%E6%9C%AC%E5%AD%90/" title="JS对象方法"><img src= "" data-lazy-src="/img/%E5%A4%A9%E7%A9%BA.jpeg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="JS对象方法"/></a><div class="content"><a class="title" href="/2022/10/11/05-%E6%A0%B8%E5%BF%83js%EF%BC%88%E6%96%B9%E6%B3%95%E5%AE%B6%E8%A7%84%EF%BC%89/JS%E5%AF%B9%E8%B1%A1%E6%96%B9%E6%B3%95%E2%80%94%E2%80%94%E9%80%9F%E8%AE%B0%E5%B0%8F%E6%9C%AC%E5%AD%90/" title="JS对象方法">JS对象方法</a><time datetime="2022-10-11T12:01:36.000Z" title="Created 2022-10-11 20:01:36">2022-10-11</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/10/11/05-%E6%A0%B8%E5%BF%83js%EF%BC%88%E6%96%B9%E6%B3%95%E5%AE%B6%E8%A7%84%EF%BC%89/JS%E6%95%B0%E7%BB%84%E6%96%B9%E6%B3%95%E2%80%94%E2%80%94%E9%80%9F%E6%9F%A5%E5%B0%8F%E6%9C%AC%E5%AD%90/" title="JS数组方法"><img src= "" data-lazy-src="/img/%E5%A4%A9%E7%A9%BA.jpeg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="JS数组方法"/></a><div class="content"><a class="title" href="/2022/10/11/05-%E6%A0%B8%E5%BF%83js%EF%BC%88%E6%96%B9%E6%B3%95%E5%AE%B6%E8%A7%84%EF%BC%89/JS%E6%95%B0%E7%BB%84%E6%96%B9%E6%B3%95%E2%80%94%E2%80%94%E9%80%9F%E6%9F%A5%E5%B0%8F%E6%9C%AC%E5%AD%90/" title="JS数组方法">JS数组方法</a><time datetime="2022-10-11T12:01:36.000Z" title="Created 2022-10-11 20:01:36">2022-10-11</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/10/11/07-%E6%A0%B8%E5%BF%83js%EF%BC%88%E6%89%8B%E5%86%99%E7%AF%87%EF%BC%89/%E5%BC%80%E5%8F%91%E5%B0%81%E8%A3%85%E5%87%BD%E6%95%B0/" title="开发封装函数"><img src= "" data-lazy-src="/img/%E5%A4%A9%E7%A9%BA.jpeg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="开发封装函数"/></a><div class="content"><a class="title" href="/2022/10/11/07-%E6%A0%B8%E5%BF%83js%EF%BC%88%E6%89%8B%E5%86%99%E7%AF%87%EF%BC%89/%E5%BC%80%E5%8F%91%E5%B0%81%E8%A3%85%E5%87%BD%E6%95%B0/" title="开发封装函数">开发封装函数</a><time datetime="2022-10-11T12:01:36.000Z" title="Created 2022-10-11 20:01:36">2022-10-11</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By 孙志豪</div><div class="framework-info"><span>Framework </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>Theme </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="Read Mode"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="Switch Between Light And Dark Mode"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="Toggle between single-column and double-column"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="Setting"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="Table Of Contents"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="Back To Top"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">Search</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  Loading the Database</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="Search for Posts" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>